package com.wd.tw.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @className: Md5Utils
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/3
 **/
public class Md5Utils {

    public static String md5(String input) {
        try {
            // 创建 MD5 消息摘要实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算摘要
            byte[] messageDigest = md.digest(input.getBytes());
            // 转换字节数组为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
